from http.server import BaseHTTPRequestHandler
from api.parse import *
import requests, json
class handler(BaseHTTPRequestHandler):
	def do_GET(self):
		self.send_response(415)
		self.send_header('Content-type', 'text/plain')
		self.end_headers()
		self.wfile.write('{"err": 1, "msg": "send POST request instead"}'.encode())
		return
	def do_POST(self):
		self.send_response(200)
		self.send_header('Content-type', 'text/plain')
		self.end_headers()
		if type_post(self) == "json":
			dat = parse_post(self)
			if "BDUSS" in dat:
				res = json.loads(requests.get('http://tieba.baidu.com/dc/common/tbs', cookies = {"BDUSS": dat["BDUSS"]}).text)
				if res["is_login"] == 1:
					self.wfile.write(f'{{"err": 0, "tbs": "{res["tbs"]}"}}'.encode())
				else:
					self.wfile.write('{"err": 1, "msg": "BDUSS invalid"}'.encode())
			else:
				self.wfile.write('{"err": 1, "msg": "BDUSS not found"}'.encode())
		else:
			self.wfile.write('{"err": 1, "msg": "wrong request type"}'.encode())
		return
